﻿package com.group6.exam.manager.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.manager.service.GradeServiceImpl;
import com.group6.exam.manager.service.IGradeService;

public class GradeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String command = request.getParameter("command");

		if ("query".equals(command)) {
			query(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("preUpdate".equals(command)) {
			preUpdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else {
			throw new RuntimeException("請求失敗" + command);
		}
	}

	/**
	 * 
	 * 班级新增
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String gradeName = request.getParameter("gradeName");

		IGradeService service = new GradeServiceImpl();
		int row = service.add(gradeName);

		if (row > 0) {
			response.sendRedirect("GradeServlet?command=query");
		} else {
			request.getRequestDispatcher(
					"view/manager/class/class-addclass.jsp").forward(request,
					response);
		}
	}

	/**
	 * 修改班级信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String gradeId = request.getParameter("gradeId");
		String gradeName = request.getParameter("gradeName");

		IGradeService service = new GradeServiceImpl();
		int row = service.updateGrade(gradeName, gradeId);

		if (row > 0) {
			response.sendRedirect("GradeServlet?command=query");
		} else {
			response.sendRedirect("GradeServlet?command=preUpdate&gradeId="
					+ gradeId);
		}
	}

	/**
	 * 修改班级前查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String gradeId = request.getParameter("gradeId");

		IGradeService service = new GradeServiceImpl();
		Map<String, Object> grade = service.preUpdateGradeById(gradeId);

		request.setAttribute("grade", grade);
		request.getRequestDispatcher("view/manager/class/class-updateclass.jsp")
				.forward(request, response);
	}

	/**
	 * 删除班级
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String gradeId = request.getParameter("gradeId");

		IGradeService service = new GradeServiceImpl();
		int row = service.deleteGradeById(gradeId);

		if (row > 0) {
			response.sendRedirect("GradeServlet?command=query");
		} else {
			response.sendRedirect("GradeServlet?command=query");
		}
	}

	/**
	 * 班级查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void query(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		IGradeService service = new GradeServiceImpl();

		// 创建调用分页业务方法所需参数
		PageModel pageModel = new PageModel();
		String pageNo = request.getParameter("currentPage");
		// 获取当前页数
		int currentPage = !"".equals(pageNo) && pageNo != null ? Integer
				.parseInt(pageNo) : 1;
		pageModel.setCurrentPage(currentPage);
		// 调用分页的业务方法
		PageModel paging = service.getPagingByPageModel(pageModel);

		request.setAttribute("pageModel", paging);
		request.getRequestDispatcher("view/manager/class/class-maintain.jsp")
				.forward(request, response);
	}
}
